Path Compression in Union-Find: Optimizing Union-Find for Faster Lookups

Union-Find (Disjoint Set Union, DSU) is used to solve set merging and element membership problems, such as connectivity judgment. Its core operations are `find` (to locate the root node) and `union` (to merge sets). The basic version uses a `parent` array to record parent nodes, but long-chain structures lead to extremely low efficiency in the `find` operation. To optimize this, **path compression** is introduced: during the `find` process, all nodes along the path are directly pointed to the root node, flattening the tree structure and making the lookup efficiency nearly O(1). Path compression can be implemented recursively or iteratively, transforming long chains into "one-step" short paths. Combined with optimizations like rank-based merging, Union-Find efficiently handles large-scale set problems and has become a core tool for solving connectivity and membership judgment tasks.

Read More
Prefix Sum: How to Quickly Calculate Interval Sum Using Prefix Sum Array?

The prefix sum array is an auxiliary array used to quickly calculate the sum of intervals. It is defined as follows: for the original array A, the prefix sum array S has S[0] = 0, and for k ≥ 1, S[k] is the sum of elements from A[1] to A[k], i.e., S[k] = S[k-1] + A[k]. For example, if the original array A = [1, 2, 3, 4, 5], its prefix sum array S = [0, 1, 3, 6, 10, 15]. The core formula for calculating the sum of an interval is: the sum of elements from the i-th to the j-th element of the original array is S[j] - S[i-1]. For example, to calculate the sum of A[2] to A[4], we use S[4] - S[1] = 10 - 1 = 9, which gives the correct result. The advantages include: preprocessing the S array takes O(n) time, and each interval sum query only takes O(1) time, resulting in an overall complexity of O(n + q) (where q is the number of queries), which is much faster than the O(qn) complexity of direct calculation. It should be noted that index alignment (e.g., adjusting the formula if the original array starts from 0), interval validity, and the space-for-time tradeoff are important considerations. The prefix sum array is implemented through "pre-accumulation".

Read More